import { request } from '@ife/utils/http';
import { ORIGIN } from '@/constants';

const headers = { 'Content-Type': 'application/json' };
export const queryProtocolList = async (params?: any) => {
    // 发起请求
    console.log('CC_ROOT: ', ORIGIN);
    let url = `${ORIGIN}/form/protocol/list/`;
    if (params) {
        const list: string[] = [];
        Object.keys(params).forEach(key => {
            if (params[key]) {
                list.push(`${key}=${params[key]}`);
            }
        });
        const str = list.join('&');
        url = `${url}?${str}`;
    }
    const init: any = { method: 'GET', headers };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || [];
};

export const queryProtocolLib = async (protocolId: number) => {
    const url = `${ORIGIN}/form/protocol/${protocolId}/`;
    const init: any = { method: 'GET', headers };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || {};
};
export const queryProtocolLinkTopics = async (protocolId: number) => {
    const url = `${ORIGIN}/form/topic/${protocolId}/list/`;
    const init: any = { method: 'GET', headers };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || [];
};
export const createProtocol = async (data: any) => {
    const url = `${ORIGIN}/form/protocol/list/`;
    const init: any = {
        method: 'POST',
        headers,
        body: JSON.stringify(data),
    };
    const res = await request(url, init);
    const result = await res.json();
    return result;
};
export const updateProtocolLib = async (protocolId: number, data: any) => {
    const url = `${ORIGIN}/form/protocol/${protocolId}/`;
    const init: any = {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    };
    const res = await request(url, init);
    const result = await res.json();
    return result;
};

export const queryProtocolDetail = async (protocolId: number) => {
    const url = `${ORIGIN}/form/case/${protocolId}/`;
    const init: any = { method: 'GET', headers };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || {};
};
export const queryCaseProtocol = async (productType: number) => {
    const url = `${ORIGIN}/form/case/protocol/?productType=${productType}&phaseType=${1}`;
    const init: any = { method: 'GET', headers };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || {};
};
